package db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;


import entity.UserConfig;


/**
 * Created by Administrator on 2017/7/19.
 */

public class UserConfigDatabaseHelper {

    private static final String TABLE_NAME = "userconfig";

    public static String getNameAndPass(String name, String psw) {
        SQLiteDatabase db = DBManager.getDatabase();
        //不存在则插入
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME + " where username = \"" + name + "\"", null);
        if (!cursor.moveToNext()) {
            db.execSQL("insert into " + TABLE_NAME + " values (\"" + name + "\", \"" + psw + "\", " + "0)");
            return "欢迎来到ci_helper";
        } else {
            //Toast.makeText(getApplicationContext(),"欢迎回来"+name , Toast.LENGTH_SHORT).show();  这个函数只能在activity里面用
            return "欢迎回来" + UserConfig.getUsername();
        }


    }

    public static void changeToVip() {
        SQLiteDatabase db = DBManager.getDatabase();
        db.execSQL("update " + TABLE_NAME + " set vipmode = 1 " + " where " + "username" + " = \"" + UserConfig.getUsername() + "\"");
    }

    public static int getVipModeByUserName() {
        final String name = UserConfig.getUsername();
        SQLiteDatabase db = DBManager.getDatabase();
        Cursor cursor = db.rawQuery("select vipmode from " + TABLE_NAME + " where username = \"" + name + "\"", null);
        if (cursor.moveToNext()) {
            int vipmode = cursor.getInt(cursor.getColumnIndex("vipmode"));
            return vipmode;
        } else return 0;
    }
}
